package com.wwwarehouse.contract.adapter.publishgoods;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.event.OperationEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 */

public class PublishSelectGoodsAdapter extends RecyclerView.Adapter<PublishSelectGoodsAdapter.ViewHolder> {

    private Context mContext = null;
    private int mHeight = 0;
    private int mWidth = 0;
    private List<ResourceItemBean.DataBean.ListBean> tagList = new ArrayList<>();
    private int numOfPage;

    public PublishSelectGoodsAdapter(Context context, List<ResourceItemBean.DataBean.ListBean> list, int height, int Width, int numOfPage) {
        this.mContext = context;
        this.mHeight = height;
        this.mWidth = Width;
        this.numOfPage = numOfPage;
        this.tagList = list;

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_select_goods_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        params.height = (mHeight - ConvertUtils.dip2px(mContext, (numOfPage + 1) * 14) - ConvertUtils.dip2px(mContext, 50)) / numOfPage; //RecyclerView高度减去item间距再减去距离按钮的高度
        //获取当前控件的布局对象
//        params.width = (int) ((mWidth - 4* ConvertUtils.dip2px(mContext, 10)) /3L);//设置当前控件布局的宽度和margin
        holder.itemView.setLayoutParams(params);
        try {
            ImageloaderUtils.getImageLoader().displayImage(tagList.get(position).getMediaList().get(0).getRelatedUrl(), holder.ivGoodsPic);
        } catch (Exception e) {
            holder.ivGoodsPic.setImageResource(R.drawable.pic_not_found);
        }
        holder.tvGoodName.setText(tagList.get(position).getDefinedName());
        try {
            holder.mCode.setText(tagList.get(position).getIdentifyList().get(0).getIdentifyCode());
        } catch (Exception e) {

        }
        if (tagList.get(position).isSelect()){
            holder.mImgEdit.setSelected(true);
            holder.mImgEdit.setBackgroundResource(R.drawable.bnt_delete_selector);
        }  else {
            holder.mImgEdit.setSelected(false);
            holder.mImgEdit.setBackgroundResource(R.drawable.bnt_add_selector);
        }

        holder.mGoodsSale.setText(mContext.getString(R.string.contract_stock) + tagList.get(position).getUnitName());
        holder.itemView.setTag(tagList.get(position));
        holder.tvDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转详情
                Bundle bundle = new Bundle();
                bundle.putString("itemType", "H_SALE");
                bundle.putLong("ukid", tagList.get(position).getDefinedUkid());
                bundle.putString("typeName", mContext.getString(R.string.contract_goods_info));
                bundle.putString("type", "0");
                ((BaseCardDeskActivity) mContext).hideSearchTitle();
                ((BaseCardDeskActivity) mContext).pushFragment("/ResourceCenter/InfomationResourcesFragment", bundle);
            }
        });

        holder.mImgEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mImgEdit.setSelected(!holder.mImgEdit.isSelected());
                if (holder.mImgEdit.isSelected()) {
                    holder.mImgEdit.setBackgroundResource(R.drawable.bnt_delete_selector);
                    EventBus.getDefault().post(new OperationEvent(tagList.get(position),"add"));
                } else {
                    holder.mImgEdit.setBackgroundResource(R.drawable.bnt_add_selector);
                    EventBus.getDefault().post(new OperationEvent(tagList.get(position),"delete"));
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return tagList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvGoodName;
        TextView mGoodsSale;
        TextView tvDetails;
        ImageView ivGoodsPic;
        ImageView mImgEdit;
        TextView mCode;

        public ViewHolder(View view) {
            super(view);
            tvGoodName = (TextView) view.findViewById(R.id.tv_goods_name);
            mGoodsSale = (TextView) view.findViewById(R.id.tv_goods_sale);
            tvDetails = (TextView) view.findViewById(R.id.tv_shop_name);
            ivGoodsPic = (ImageView) view.findViewById(R.id.iv_goods_pic);
            mImgEdit = (ImageView) view.findViewById(R.id.img_edit);
            mCode = (TextView) view.findViewById(R.id.tv_goods_code);
        }
    }



}
